<template>
  <div>
    <el-dialog
      title="新增员工"
      :visible="isShowDialog"
      width="40%"
      @close="closeDialog"
    >
      <!-- 表单 -->
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="form.username"
            style="width: 80%"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            v-model="form.mobile"
            style="width: 80%"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            style="width: 80%"
            placeholder="请选择入职时间"
          />
        </el-form-item>

        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="form.formOfEmployment"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in employees.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="form.workNumber"
            style="width: 80%"
            placeholder="请输入工号"
          />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            style="width: 80%"
            placeholder="请选择部门"
            @focus="getTreeList"
          />
          <!-- 使用树形结构
          <el-row v-show="isshowTree" type="flex" justify="start">
            <el-tree
              ref="tree"
              :data="transTreeList"
              :props="{ label: 'name' }"
              @node-click="selectNode"
            ></el-tree>

          </el-row> -->

          <!-- 自己封装的组件 -->
          <TreeList :trans-tree-list="transTreeList" :isshow-tree="isshowTree" @getInputName="selectNode"> </TreeList>

        </el-form-item>
        <el-form-item label="转正时间">
          <el-date-picker
            v-model="form.correctionTime"
            style="width: 80%"
            placeholder="请选择转正时间"
          />
        </el-form-item>
      </el-form>
      <!-- footer插槽 -->
      <span slot="footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="addEmployee">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { tranListToTreeData, parseTime } from '@/utils/index' // 引入把树形结构数据转换的方法
import { getDepartmentsAPI } from '@/api/department.js' // 引入department.js为了调用获取树形结构数据的APi
import employees from '@/api/constant/employees' // 引入的数据常量
import { addEmployeeAPI } from '@/api/employees'
export default {
  props: {
    isShowDialog: {
      type: Boolean, Infinity
    }
  },
  data () {
    return {
      isshowTree: false, // 是否显示树形结构
      transTreeList: [], // 处理过的树形数据

      employees, // 数据常量 聘用形式(1代表正式,2代表非正式)
      form: {
        username: '',			// 姓名
        mobile: '', //	手机号
        formOfEmployment: '',	// 聘用形式
        workNumber: '',			// 工号
        departmentName: '',	//	组织名称
        timeOfEntry: '', //	入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '请填写姓名', trigger: ['blur', 'change'] }],
        mobile: [
          { required: true, message: '请填写手机号', trigger: ['blur', 'change'] },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '请输入有效的手机号', trigger: ['blur'] }
        ],
        formOfEmployment: [{ required: true, message: '请填写聘用形式', trigger: ['blur', 'change'] }],
        workNumber: [{ required: true, message: '请填写工号', trigger: ['blur', 'change'] }],
        departmentName: [{ required: true, message: '请填写部门', trigger: ['blur', 'change'] }],
        timeOfEntry: [{ required: true, message: '请填写入职时间', trigger: ['blur', 'change'] }]
      }
    }
  },
  // created () {
  //   this.getTreeList()
  // },
  methods: {
    // 关闭弹出层
    closeDialog () {
      this.$refs.form.resetFields()
      this.$emit('update:isShowDialog', false)
    },
    // 获取焦点时展示树形结构
    async getTreeList () {
      console.log(1)

      this.isshowTree = true
      // 调用查询企业的部门列表API
      const res = await getDepartmentsAPI()
      this.transTreeList = tranListToTreeData(res.depts)
    },
    selectNode (node) {
      this.form.departmentName = node
      this.isshowTree = false
    },
    // 点击确定添加新的员工
    addEmployee () {
      this.$refs.form.validate(async (isok) => {
        if (isok) {
          // 先处理el-date-picker组件的时间
          this.form.timeOfEntry = parseTime(this.form.timeOfEntry, '{y}-{m}-{d}') // 第一个参数是要被格式化的时间对象,第二个参数是格式化的格式
          if (this.form.correctionTime) { // 如果输入了转正时间就格式化
            this.form.correctionTime = parseTime(this.form.correctionTime, '{y}-{m}-{d}')
          }
          const res = await addEmployeeAPI(this.form)
          console.log(res)
          this.$message.success('添加成功')
          this.closeDialog()
          // 通过this.$parent可以直接调用父组件的方法
          this.$parent.getEmployeesList()
          this.$refs.form.resetFields()
          this.form.correctionTime = ''
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
